﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.ObjectModel;

namespace ASPItalia.ModelVirtualCasting.Fake
{
    public class NewsRepository : BaseRepository<INewsRepository>, INewsRepository
    {
        private List<News> list;

        public NewsRepository()
        {
            string newsBody = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a imperdiet dui. Proin erat metus, vulputate a laoreet vel, commodo sit amet nisi. Aliquam erat volutpat. Praesent leo dolor, aliquam sit amet mattis a, rhoncus a arcu. Sed augue urna, vestibulum vitae ultrices et, dictum vel leo. Suspendisse pulvinar, lectus eu volutpat molestie, nisi purus rutrum nunc, nec iaculis dolor dolor eu neque. Nam faucibus bibendum viverra. Nunc commodo tortor eu mi suscipit blandit. Curabitur egestas quam nec velit rhoncus ornare. In sit amet ante neque, et gravida tortor. In egestas nunc sit amet tortor blandit elementum. Fusce sagittis dui ut magna sagittis posuere. Ut lobortis interdum ligula in dictum. Praesent velit felis, fermentum in pharetra ut, aliquam vel felis. Donec vel urna eget ipsum adipiscing tincidunt vitae a tellus.";

            this.list = new List<News>();
            for (int i = 0; i < 10; i++)
            {
                list.Add(new News
                             {
                                 NewsTitle = "Questa è la notizia " + (i + 1),
                                 NewsText = newsBody,
                                 IsPublished = true,
                                 NewsDate = new DateTime(2009, 11, 20 + i),
                                 PublicationDate = new DateTime(2009, 11, 20 + i),
                                 NewsID = i + 1
                             });
            }

            list[9].NewsTitle = "Hacking news! <script type='text/javascript'>alert('Hacked!!')</script>";
        }

        public void InsertOnSubmit(News entity)
        {
        }

        public void Update(News entity)
        {
        }

        public void DeleteOnSubmit(News entity)
        {
        }

        public void SubmitChanges()
        {
        }

        public IQueryable<News> GetLastNews(int number)
        {
            var res = from n in this.list
                      orderby n.NewsDate descending
                      select n;

            return res.Take(5).AsQueryable();
        }

        public IQueryable<News> GetAll()
        {
            return null;
        }

        public IQueryable<News> Where(Expression<Func<News, bool>> predicate)
        {
            return null;
        }

        public long Count(Expression<Func<News, bool>> predicate)
        {
            return 0L;
        }

        #region INewsRepository Members


        public News GetByID(int id)
        {
            return this.list.Where(n => n.NewsID == id).Single();
        }

        #endregion

				#region IReadOnlyRepository<INewsRepository,News> Members


				public INewsRepository EnableLazyLoading()
				{
					throw new NotImplementedException();
				}

				public INewsRepository DisableLazyLoading()
				{
					throw new NotImplementedException();
				}

				#endregion
		}
}
